<template>
  <div class="w-card content-box" v-if="!isDetail">
    <div class="title-box">
      <span class="title">提现订单</span>
      <el-button style="margin-left:auto;" @click="exportHandle(1)">导出</el-button>
    </div>
    <div class="my-search-box">
      <div>
        <span>订单ID：</span>
        <el-input v-model="searchForm.sysOrderId" clearable placeholder="请输入订单ID" />
      </div>
      <div>
        <span>代理商：</span>
        <el-input v-model="searchForm.shopName" clearable placeholder="请输入代理商名称" />
      </div>
      <div>
        <span>提现状态：</span>
        <el-select v-model="searchForm.orderStatus">
          <el-option v-for="item in orderStatusOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </div>
      <div>
        <span>提现方式：</span>
        <el-select v-model="searchForm.payType">
          <el-option v-for="item in payTypeOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </div>
    </div>
    <div class="my-search-box">
      <div class="datebox">
        <span>操作时间：</span>
        <el-date-picker v-model="searchForm.date" type="datetimerange" value-format="x" start-placeholder="开始时间"
          end-placeholder="结束时间" />
      </div>
      <div class="f-ml-20">
        <el-button @click="searchHandle" type="primary">查询</el-button>
        <el-button @click="resetHandle">重置</el-button>
      </div>
    </div>
    <div class="table-box">
      <el-table :max-height="tableHeight" :header-cell-style="{background:'#d6dee5',color:'#000000',fontSize:'14px'}"
        :cell-style="{fontSize:'14px',color:'#7b7b7d'}" :data="tableData" stripe @selection-change="handleSelectionChange">
        <el-table-column type="index" width="55" label="序号" align="center" />
        <el-table-column type="selection" width="55" />
        <el-table-column prop="sysOrderId" label="订单ID" align="center" min-width="120">
          <template v-slot="scope">
            <el-link type="primary" @click="toDetail(scope.row.sysOrderId)">{{ scope.row.sysOrderId || '' }}</el-link>
          </template>
        </el-table-column>
        <el-table-column prop="shopName" label="代理商" align="center">
          <template v-slot="scope">
            {{ scope.row.shopName || '' }}
          </template>
        </el-table-column>
        <el-table-column prop="payType" label="提现方式" align="center">
          <template v-slot="scope">
            {{ scope.row.payType == 2 ? '微信' : scope.row.payType == 3 ? '支付宝' : scope.row.payType == 11 ? '银行卡' : '' }}
          </template>
        </el-table-column>
        <el-table-column prop="toAccount" label="收款账号" align="center" min-width="140">
          <template v-slot="scope">
            {{ scope.row.toAccount || '' }}
          </template>
        </el-table-column>
        <el-table-column prop="totalMoney" label="提现金额(元)" align="center" min-width="120">
          <template v-slot="scope">
            {{ numFormat(scope.row.totalMoney || '') }}
          </template>
        </el-table-column>
        <el-table-column prop="serviceMoney" label="手续费(元)" align="center" min-width="120">
          <template v-slot="scope">
            {{ numFormat(scope.row.serviceMoney || '') }}
          </template>
        </el-table-column>
        <el-table-column prop="actualMoney" label="实际到账(元)" align="center" min-width="120">
          <template v-slot="scope">
            {{ numFormat(scope.row.actualMoney || '') }}
          </template>
        </el-table-column>
        <el-table-column prop="orderStatus" label="状态" align="center" width="100">
          <template v-slot="scope">
            <span :class="'oStatus oStatus'+scope.row.orderStatus">{{ getOrderStatus(scope.row.orderStatus) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="操作时间" align="center" width="180">
          <template v-slot="scope">
            {{ scope.row.createTime ? getDateTime(scope.row.createTime)[6] : '' }}
          </template>
        </el-table-column>
      </el-table>
      <div class="my-table-footer">
        <div>
          <el-button :disabled="chooseList.length == 0" @click="exportHandle(2)">批量导出</el-button>
        </div>
        <el-pagination style="margin-left:auto;" v-model:currentPage="page" v-model:page-size="size"
          :page-sizes="[10, 20, 50, 100]" small :background="true" layout="total, sizes, prev, pager, next, jumper" :total="total"
          @size-change="handleSizeChange" @current-change="handleCurrentChange" />
      </div>
    </div>
  </div>
  <router-view></router-view>
</template>

<script>
import { onMounted, reactive, toRefs, watch } from 'vue'
import api from '../api'
import { getWindowWidthAndHeight, warningDeal, numFormat, getDateTime, BlobDownLoad } from '@/utils/utils'
import { useRoute, useRouter } from 'vue-router'
export default {
  setup() {
    const route = useRoute()
    const router = useRouter()

    const state = reactive({
      isDetail: false,

      page: 1,
      size: 10,
      total: 0,
      tableHeight: 0,
      tableData: [],
      chooseList: [],

      searchForm: {},

      orderStatusOptions: [
        {
          value: 1,
          label: '发起提现',
        },
        {
          value: 2,
          label: '已关闭',
        },
        {
          value: 3,
          label: '提现失败',
        },
        {
          value: 4,
          label: '提现成功',
        },
        {
          value: 5,
          label: '待人工处理',
        },
      ],
      payTypeOptions: [
        {
          value: 2,
          label: '微信',
        },
        {
          value: 3,
          label: '支付宝',
        },
        {
          value: 11,
          label: '银行卡',
        },
      ],
    })

    watch(() => route.path, (newVal, oldVal) => {
      setIsDetail()
    })

    function setIsDetail() {
      state.isDetail = (route.path != '/orderMng/cashOrder')
    }

    function getOrderStatus(orderStatus) {
      let res = ''
      state.orderStatusOptions.map(item => {
        if (item.value == orderStatus) {
          res = item.label
        }
      })
      return res
    }

    function getTableData() {
      const obj = {
        page: state.page,
        size: state.size,
        ...state.searchForm,
        timeMin: (state.searchForm.date && state.searchForm.date[0]) ? state.searchForm.date[0] : null,
        timeMax: (state.searchForm.date && state.searchForm.date[1]) ? state.searchForm.date[1] : null,
        orderBy: 'id desc',
      }
      api.withdrawOrderList(obj).then(res => {
        if (res.code == '200') {
          state.tableData = res.data.list
          state.total = res.data.total
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function handleCurrentChange(e) {
      state.page = e
      getTableData()
    }

    function handleSizeChange(e) {
      state.size = e
      getTableData()
    }

    function handleSelectionChange(e) {
      state.chooseList = e
    }

    function searchHandle() {
      state.page = 1
      getTableData()
    }

    function resetHandle() {
      state.searchForm = {}
      searchHandle()
    }

    function toDetail(sysOrderId) {
      router.push({ path: '/orderMng/cashOrder/orderDetail', query: { sysOrderId } })
    }

    function exportHandle(type) {
      let obj = {}
      switch (type) {
        case 1:
          obj = {
            page: state.page,
            size: state.size,
            ...state.searchForm,
            timeMin: (state.searchForm.date && state.searchForm.date[0]) ? state.searchForm.date[0] : null,
            timeMax: (state.searchForm.date && state.searchForm.date[0]) ? state.searchForm.date[0] : null,
            orderBy: 'id desc',
          }
          break
        case 2:
          obj = {
            page: state.page,
            size: state.size,
            sysOrderId: getSysOrderId(state.chooseList).join(','),
          }
          break
      }
      api.withdrawOrderDown({ ...obj, responseType: "blob" }).then(res => {
        const _fileName = decodeURIComponent(res.headers['content-disposition'].split(';')[1].split('=')[1])
        BlobDownLoad(res, _fileName)
      }).catch(err => {
        console.log(err)
      })
    }

    function getSysOrderId(list) {
      const myArr = []
      list && list.map(item => {
        myArr.push(item.sysOrderId)
      })
      return myArr
    }

    onMounted(() => {
      let winHeight = getWindowWidthAndHeight()
      state.tableHeight = winHeight.windowHeight - 330
      state.searchForm.shopName = route.query.shopName || null
      setIsDetail()
      getTableData()
    })

    return {
      ...toRefs(state),
      exportHandle,
      handleCurrentChange,
      handleSizeChange,
      searchHandle,
      resetHandle,
      numFormat,
      getDateTime,
      getOrderStatus,
      handleSelectionChange,
      toDetail,
    }
  }
}
</script>

<style lang="scss" scoped>
.content-box {
  height: 100%;
  overflow: hidden;
  overflow-y: auto;
  box-sizing: border-box;
  padding: 20px;
  .title-box {
    display: flex;
    align-items: center;
    > .title {
      font-size: 16px;
      font-weight: bold;
    }
  }
  .my-search-box {
    box-sizing: border-box;
    padding: 20px 20px 20px 0;
    padding-bottom: 0;
    font-size: 14px;
    display: flex;
    align-items: center;
    gap: 10px;

    > div {
      width: 350px;
      display: flex;
      align-items: center;

      > span {
        flex-shrink: 0;
        width: 120px;
        text-align: right;
      }
    }

    .datebox {
      width: 450px;
    }
  }
  .table-box {
    margin-top: 10px;
    .my-table-footer {
      display: flex;
      align-items: center;
      margin-top: 10px;
      flex-wrap: wrap;
      gap: 10px;
    }
  }
}

.oStatus {
  display: flex;
  align-items: center;
  gap: 5px;
  &::before {
    content: "";
    display: block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    flex-shrink: 0;
  }
}
.oStatus1 {
  &::before {
    background-color: #e6a23c;
  }
}
.oStatus2 {
  &::before {
    background-color: #b7b9bd;
  }
}
.oStatus3 {
  &::before {
    background-color: #f04134;
  }
}
.oStatus4 {
  &::before {
    background-color: #00a854;
  }
}
.oStatus5 {
  &::before {
    background-color: #e6a23c;
  }
}
</style>